<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	#import {
		position: relative;
		top: 1.4px;
	}
	#import-class {
		position: relative;
		top: 1.4px;
	}
</style>
<div class="page-header">
	<h1>
		年级管理
		<!-- 教师权限不可编辑 -->
		<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
			<!-- 非同步专业才支持导入 -->
			<?php if(! $rmxSchool['school_from']): ?>
			<!-- 和教师权限不可添加 -->
				<?php if(! $rmxSchool['school_from'] && $rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
					<a href="<?= admin_url('classes/grade_edit') ?>" class="btn btn-info a-custom">
						<i class="ace-icon fa fa-plus"></i> 新增年级
					</a>
				<?php endif ?>
				<span class="dropdown dropdown-custom">
					<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
						<i class="ace-icon fa fa-database"></i> 模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
					</div>
					<ul class="dropdown-menu">
						<li>
							<a href="javascript:" id="import">
								<i class="ace-icon fa fa-upload"></i> 导入年级数据
							</a>
						</li>
						<li>
							<a href="https://resource.rongmixiao.com/2020/05/05/f329f09333f89ded2b4e414b10d288a5.xls" target="_blank">
								<i class="ace-icon fa fa-download"></i> 导出年级数据模板
							</a>
						</li>
						<li>
							<a href="javascript:" id="import-class">
								<i class="ace-icon fa fa-upload"></i> 导入班级数据
							</a>
						</li>
						<li>
							<a href="https://resource.rongmixiao.com/2020/05/05/97498d934fbcc9d72a3012dbcd23cb6d.xls" target="_blank">
								<i class="ace-icon fa fa-download"></i> 导出班级数据模板
							</a>
						</li>
					</ul>
				</span>
				<?php if($existClassData): ?>
					<a href="<?= admin_url('classes/export_class_data') ?>" class="btn btn-info a-custom" target="_blank">
						<i class="ace-icon fa fa-download"></i> 导出班级数据
					</a>
				<?php endif ?>
			<?php endif ?>
		<?php endif ?>	
	</h1>
</div>

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>所属年级</th>
				<th>年级别名</th>
				<th>入学年份</th>
				<!-- <th>升级日期</th> -->
				<!-- <th>排序序号</th> -->
				<th>创建时间</th>
				<!-- <th>班级管理</th> -->
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 新增年级弹窗 -->
<div id="modal-grade" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					年级管理
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="grade-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">所属校区：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" disabled="" value="<?=$campusInfo['campus_name']?>" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">年级名称：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="grade_name" placeholder="请填写年级名称"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-5 control-label no-padding-right">排序（数值越大越靠前）：</label>
								<div class="col-sm-4">
									<input class="form-control" type="number" name="sort_level" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-grade">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 保存年级按钮
		var gradeForm = $("#grade-form");
		$("#save-grade").click(function(){
			gradeForm.submit();
		})
		var submitUrl = "<?=admin_url('classes/save_grade_action')?>";
		form_submit(gradeForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
		// 编辑年级
		$("body").on("click", ".edit-grade", function(){
			gradeForm.find("[name=id]").val($(this).data("id"))
			gradeForm.find("[name=grade_name]").val($(this).data("name"))
			gradeForm.find("[name=sort_level]").val($(this).data("sort-level"));

			// 如果是同步的数据，设置只读
			if($(this).data("xid")){
				gradeForm.find("[name=grade_name]").attr("readonly","readonly")
			} else{
				gradeForm.find("[name=grade_name]").removeAttr("readonly")
			}
		})

		// 删除年级
		$("body").on("click", ".delete-grade", function(){
			// 如果是同步的数据，不可删除
			if($(this).data("xid")){
				layer.msg('不可删除同步的数据');
				return;
			}
			var id = $(this).data("id");
			var name = $(this).data("name");
			layer.confirm('确定删除：' + name + '吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('classes/delete_grade')?>";
				var postData = {"id": id};
				ajax_post(postUrl, postData, function(res){
					window.location.reload();
				})
			});
		})

		// 年级表格
		$('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('classes/get_grade_list')?>',
			"columns": [
				{"data": "default_grade_name"},
				{"data": "grade_name"},
				{"data": "year_of_admission"},
				// {"data": "auto_upgrade_date"},
				// {"data": "sort_level"},
				{"data": "createtime"},
	            // {"data": function(ret){
	            // 	return '<a class="green" href="<?=admin_url('classes/classes')?>/'+ ret.id +'"><i class="fa fa-gears"></i> 查看</a>';
	            // }},
	            {"data": function(ret){
					var action = '<a class="green" href="<?=admin_url('classes/classes')?>/'+ ret.id +'"><i class="fa fa-gears"></i> 查看班级</a>';
					// 学校管理员才能编辑
					<?php if($this->rmxUser['user_type'] == USER_TYPE_SCHOOL): ?>
						// ‘功能课室’不可编辑，毕业年级不可编辑
						// if(ret.school_id != 0 && ret.grade_name.indexOf('毕业年级') === -1) {
						if(ret.school_id != 0) {
							// action += '<a class="edit-grade green" href="#modal-grade" data-toggle="modal" data-id="'+ ret.id +'" data-name="'+ ret.grade_name +'" data-xid="' + ret.x_grade_id + '" data-sort-level="' + ret.sort_level + '"><i class="fa fa-edit"></i> 修改</a>';
							action += '<a class="edit-grade green ml20" href="<?= admin_url('classes/grade_edit') ?>/' + ret.id + '"><i class="fa fa-edit"></i> 修改年级</a>';
							action += '<a class="delete-grade red ml20" href="javascript:;" data-id="'+ ret.id +'" data-xid="' + ret.x_grade_id + '" data-name="' + ret.grade_name + '"><?php if(! $rmxSchool['school_from']): ?><i class="fa fa-times"></i> 删除年级</a><?php endif ?>';
						}	
					<?php endif ?>
	                return action;
	            }},
			]
		});
		// 导入年级
		var upFromurl = '<?=UP_FORM_URL?>';
		var signature = '<?=$signature?>';
		var policy = '<?=$policy?>';
		$("#import").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				ajax_post("<?= admin_url('import/import_grade') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 导入班级
		$("#import-class").uploader({
			"server": upFromurl,
			"authorization": signature,
			"policy": policy,
			// 只支持上传 xls
			"accept": "xls",
			"callback": function(response) {
				// 上传后的文件名
				var fileLink = upyunUrl + response.url;
				ajax_post("<?= admin_url('import/import_class') ?>", { fileLink: fileLink}, function(res) {
					// 刷新页面
					location.reload();
				});
			}
		});
		// 设置选择文件按钮大小
		setTimeout(function(){
			$(".webuploader-pick").next().width(158).height(32);
		}, 100);
	})
</script>